home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / jpeg / jwrsgi.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  172 lines

  1. /*
  2.  * jwrsgi.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to write output images in SGI format.
  9.  *
  10.  * These routines may need modification for non-Unix environments or
  11.  * specialized applications.  As they stand, they assume output to
  12.  * an ordinary stdio stream.
  13.  *
  14.  *
  15.  * These routines use the SGI image library functions found in -limage
  16.  *
  17.  *                              Paul Haeberli - 1993
  18.  */
  19.  
  20. #include "jinclude.h"
  21.  
  22. #ifdef SGI_SUPPORTED
  23.  
  24. #include "gl/image.h"
  25.  
  26. #ifndef EIGHT_BIT_SAMPLES
  27.   Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
  28. #endif
  29.  
  30. LOCAL short *rbuf;
  31. LOCAL short *gbuf;
  32. LOCAL short *bbuf;
  33. LOCAL IMAGE *image;
  34. LOCAL int cury;
  35. LOCAL int outf;
  36.  
  37. /*
  38.  * Write the file header.
  39.  */
  40.  
  41. LOCAL void
  42. write_header (decompress_info_ptr cinfo, int num_colors)
  43. /* Create and write a SGI header */
  44. {
  45.   int xsize, ysize;
  46.   
  47.   xsize = cinfo->image_width;
  48.   ysize = cinfo->image_height;
  49.   outf = dup(fileno(cinfo->output_file));
  50.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  51.     image = (IMAGE *)fiopen(outf,"w",RLE(1),2,xsize,ysize,1);
  52.     rbuf = (short *)malloc(xsize*sizeof(short));
  53.   } else {
  54.     image = (IMAGE *)fiopen(outf,"w",RLE(1),3,xsize,ysize,3);
  55.     rbuf = (short *)malloc(xsize*sizeof(short));
  56.     gbuf = (short *)malloc(xsize*sizeof(short));
  57.     bbuf = (short *)malloc(xsize*sizeof(short));
  58.   }
  59.   if(!image)
  60.     ERREXIT(cinfo->emethods, "Could not write SGI header");
  61.   cury = 0;
  62. }
  63.  
  64.  
  65. /*
  66.  * Init output
  67.  */
  68.  
  69. METHODDEF void
  70. output_init (decompress_info_ptr cinfo)
  71. {
  72.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  73.     write_header(cinfo, 0);
  74.   } else if (cinfo->out_color_space == CS_RGB) {
  75.     write_header(cinfo, 0);
  76.   } else {
  77.     ERREXIT(cinfo->emethods, "SGI output must be grayscale or RGB");
  78.   }
  79. }
  80.  
  81.  
  82. /*
  83.  * Write some pixel data.
  84.  */
  85.  
  86. METHODDEF void
  87. put_RGB_pixel_rows (decompress_info_ptr cinfo, int num_rows,
  88.         JSAMPIMAGE pixel_data)
  89. {
  90.   register JSAMPROW ptr0, ptr1, ptr2;
  91.   register long col;
  92.   long width = cinfo->image_width;
  93.   int row;
  94.   
  95.   for (row = 0; row < num_rows; row++) {
  96.     ptr0 = pixel_data[0][row];
  97.     ptr1 = pixel_data[1][row];
  98.     ptr2 = pixel_data[2][row];
  99.     for (col = 0; col < width; col++) {
  100.       rbuf[col]  = GETJSAMPLE(*ptr0);
  101.       gbuf[col]  = GETJSAMPLE(*ptr1);
  102.       bbuf[col]  = GETJSAMPLE(*ptr2);
  103.       ptr0++;
  104.       ptr1++;
  105.       ptr2++;
  106.     }
  107.     putrow(image,rbuf,image->ysize-1-cury,0);
  108.     putrow(image,gbuf,image->ysize-1-cury,1);
  109.     putrow(image,bbuf,image->ysize-1-cury,2);
  110.     cury++;
  111.   }
  112. }
  113.  
  114. METHODDEF void
  115. put_BW_pixel_rows (decompress_info_ptr cinfo, int num_rows,
  116.         JSAMPIMAGE pixel_data)
  117. {
  118.   register JSAMPROW ptr0;
  119.   register long col;
  120.   long width = cinfo->image_width;
  121.   int row;
  122.   
  123.   for (row = 0; row < num_rows; row++) {
  124.     ptr0 = pixel_data[0][row];
  125.     for (col = 0; col < width; col++) {
  126.       rbuf[col]  = GETJSAMPLE(*ptr0);
  127.       ptr0++;
  128.     }
  129.     putrow(image,rbuf,image->ysize-1-cury,0);
  130.     cury++;
  131.   }
  132. }
  133.  
  134. /*
  135.  * Finish up at the end of the file.
  136.  */
  137.  
  138. METHODDEF void
  139. output_term (decompress_info_ptr cinfo)
  140. {
  141.   /* No work except to make sure we wrote the output file OK */
  142.   iclose(image);
  143.   fflush(cinfo->output_file);
  144.   if (ferror(cinfo->output_file))
  145.     ERREXIT(cinfo->emethods, "Output file write error");
  146.   free(rbuf);
  147.   if (cinfo->out_color_space == CS_RGB) {
  148.     free(gbuf);
  149.     free(bbuf);
  150.   }
  151. }
  152.  
  153. /*
  154.  * The method selection routine for SGI format output.
  155.  * This should be called from d_ui_method_selection if SGI output is wanted.
  156.  */
  157.  
  158. GLOBAL void
  159. jselwsgi (decompress_info_ptr cinfo)
  160. {
  161.   cinfo->methods->output_init = output_init;
  162.   if (cinfo->out_color_space == CS_GRAYSCALE)
  163.     cinfo->methods->put_pixel_rows = put_BW_pixel_rows;
  164.   else if (cinfo->out_color_space == CS_RGB)
  165.     cinfo->methods->put_pixel_rows = put_RGB_pixel_rows;
  166.   else
  167.     ERREXIT(cinfo->emethods, "SGI files can only be BW or RGB");
  168.   cinfo->methods->output_term = output_term;
  169. }
  170.  
  171. #endif /* SGI_SUPPORTED */
  172.